package com.intelligentmeterreadingsys.waterlist.querywaterlist.activity;

import java.util.List;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.UserWaterVO;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;

import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

/**
 * 表册查询主界面
 * 
 * @author king
 * 
 */
public class QueryWaterListActivity extends ActivityGroup {
	private LinearLayout container = null;

	private RadioGroup radiogroup = null;

	private RadioButton radio2 = null;//水表信息

	private RadioButton radio3 = null;//数据统计

	private RadioButton radio4 = null;//选择条件
	
	private RadioButton radio1=null;//水表列表信息
	
	private List<UserWaterVO> list;

	public List<UserWaterVO> getList() {
		return list;
	}

	public void setList(List<UserWaterVO> list) {
		this.list = list;
	}

	private String waterlistid;
	private String usernumber;
	public String getWaterlistid() {
		return waterlistid;
	}

	public void setWaterlistid(String waterlistid) {
		this.waterlistid = waterlistid;
	}

	private String bandingstatus;
	private String copystatus;
	private String wateradds;
	private String waterproperties;
	private String waterid;
	

	public String getWaterid() {
		return waterid;
	}

	public void setWaterid(String waterid) {
		System.out.println(waterid+"group");
		this.waterid = waterid;
	}

	

	public String getUsernumber() {
		return usernumber;
	}

	public void setUsernumber(String usernumber) {
		System.out.println(usernumber);
		this.usernumber = usernumber;
	}

	
	public String getBandingstatus() {
		return bandingstatus;
	}

	public void setBandingstatus(String bandingstatus) {
		this.bandingstatus = bandingstatus;
	}

	public String getCopystatus() {
		return copystatus;
	}

	public void setCopystatus(String copystatus) {
		System.out.println(copystatus);
		this.copystatus = copystatus;
	}

	public String getWateradds() {
		return wateradds;
	}

	public void setWateradds(String wateradds) {
		this.wateradds = wateradds;
	}

	public String getWaterproperties() {
		return waterproperties;
	}

	public void setWaterproperties(String waterproperties) {
		this.waterproperties = waterproperties;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.querywaterlist);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
		inti();

	}

	/**
	 * 界面切换
	 * 
	 * @param id
	 * @param activityClass
	 */
	public void launchActivity(String id, Class<?> activityClass, Bundle bundle) {
		container.removeAllViews();

		Intent intent = new Intent(QueryWaterListActivity.this, activityClass);
		
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

		Window window = getLocalActivityManager().startActivity(id, intent);
		View view = window.getDecorView();
		container.addView(view);
	}

	/**
	 * 初始化控件并添加事件
	 */
	public void inti() {
		container = (LinearLayout) findViewById(R.id.container1);
		radiogroup = (RadioGroup) findViewById(R.id.querywater_main_radio);
        radio1=(RadioButton)findViewById(R.id.waterlist_info);
		radio2 = (RadioButton) findViewById(R.id.waterlist_waterinfo);
		radio3 = (RadioButton) findViewById(R.id.waterlist_querywaterlist);
		radio4 = (RadioButton) findViewById(R.id.waterlist_querycondition);
		launchActivity(""+System.currentTimeMillis(),
				SettingStatisticsInfoActivity.class, null);
		radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == radio2.getId()) {                    
					launchActivity("WaterinfoActivity",
							WaterinfoActivity.class, null);
				} else if (checkedId == radio3.getId()) {
					launchActivity("SettingStatisticsInfoActivity",
							SettingStatisticsInfoActivity.class, null);
				}else if(checkedId==radio1.getId()){
					launchActivity("ListViewActivity", ListViewActivity.class, null);
					ListViewActivity act=(ListViewActivity)getCurrentActivity();
					ListView listview=act.getListView();
					 list=act.getList();
					 act.flip();

					
					listview.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1,
								int arg2, long arg3) {
							if(list==null||list.size()<1){
								Toast.makeText(QueryWaterListActivity.this, "没有查到水表哟!", 1000).show();
								return;
							}
							 waterid=  list.get(arg2).getWaterid();
						
							radio2.setChecked(true);
						}
						
					});
					
					
				}else if (checkedId == radio4.getId()) {
					launchActivity("SetQueryConditionActivity",
							SetQueryConditionActivity.class, null);
					final SetQueryConditionActivity set = (SetQueryConditionActivity) getCurrentActivity();
					Button button = set.button1;
					button.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							waterlistid=set.getWaterlistid();
							usernumber=set.edit1.getEditableText().toString();
							bandingstatus=set.bandingstatus;
							waterproperties=set.edit2.getEditableText().toString();
							copystatus=set.getUsertype();
							wateradds= set.edit4.getEditableText().toString();							
							launchActivity("ListViewActivity",
									ListViewActivity.class, null);
							ListViewActivity activity=(ListViewActivity)getCurrentActivity();
							activity.queryDater();
							radio1.setChecked(true);

						}
						
					});

				}

			}
		});

	}

	/**
	 * 打开子Activity的菜单事件
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_MENU){
			getCurrentActivity().openOptionsMenu(); 
		}
		return super.onKeyDown(keyCode, event);
	}

}
